$(function () {
    queryConfigList();
    $("body").on('click',".config-info-btn",function () {
        var title = $(this).data('original-title');
        var id = $(this).data('id');
        parent.layer.open({
            type: 2,
            title: '<strong>'+title+'</strong><small class="ml10"></small>',
            shadeClose: false,
            shade: [0.3, '#000000'],
            maxmin: true, //开启最大化最小化按钮
            area: ['900px', '520px'],
            btn: ['关闭'],
            content: baseloaction + '/admin/sys/config/showConfigInfo?id='+id,
            yes: function(index, layero){
                parent.layer.close(index);
            }
        });
    }).on('click','.show-config-info-box',function () {//查看配置详情
        var _this = $(this);
        var id = _this.data('id');
        var title = _this.data('original-title');
        parent.layer.open({
            type: 2,
            title: '<strong>'+title+'</strong><small class="ml10"></small>',
            shadeClose: false,
            shade: [0.3, '#000000'],
            maxmin: true, //开启最大化最小化按钮
            area: winArea,
            btn: ['关闭'],
            content: baseloaction + '/admin/manage/config/showConfigInfo?id='+id,
            yes: function(index, layero){
                parent.layer.close(index);
            }
        });
    });
    /**
     * 加载数据
     */
    function queryConfigList() {
        var obj = {
            url: baseloaction + '/admin/manage/config/ajaxConfigList',//请求URL
            contextId:'config-list-context',//响应内容页面设置内容ID
            params:null,//请求参数
            pageCall:queryConfigList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            callback:function () {//数据加载完成回调
                var funType = $("#funType").val();
                if(funType !=1){
                    $("#config-list-context li").removeClass('active');
                    $("#config-list-context .tab-content .tab-pane").removeClass('active');
                    $("a[href='#tab-"+funType+"']").parent('li').addClass("active");
                    $("#tab-"+funType).addClass("active");
                }
            }
        };
        ajaxGetListData(obj);
    }

    function ajaxGetListData(obj) {
        var _params = '';
        var pageNo = 1;
        if (typeof obj.pageNo != 'undefined' && obj.pageNo != 'undefined' && obj.pageNo != null && obj.pageNo != 'undefined') {
            pageNo = obj.pageNo;
        }
        if (!$.isEmptyObject(obj.params) && $.trim(obj.params) != '') {
            _params += obj.params;
        }
        $.ajax({
            url: obj.url+"?currentPage="+pageNo,
            data: _params,
            dataType: 'text',
            type:'post',
            contentType:'application/json;charset=utf-8',
            async: false,
            beforeSend: function (XHR) {
                var loadText = '<div class="icLoading"><span>&nbsp;</span></div>';
                $("#" + obj.contextId).html(loadText);
                return true;
            },
            success: function (result) {
                $("#" + obj.contextId).html(result);
                setEndTrTd();
                // tooltips
                $('[data-toggle="tooltip"]').tooltip();
                $("#select-page-no").change(function () {
                    if (obj.pageCall != 'undefined' && $.isFunction(obj.pageCall)) {
                        obj.pageCall(this.value);
                    }
                });

                $("#page-list-box>li").each(function () {
                    var _this = $(this);
                    if (!_this.hasClass('disabled') && !_this.hasClass('active')) {
                        _this.click(function () {
                            var _pageNo = $(this).attr('data-no');
                            if (obj.pageCall != 'undefined' && $.isFunction(obj.pageCall)) {
                                obj.pageCall(_pageNo);
                            }
                        });
                    }
                });
                var _checks = $("#" + obj.contextId + " .i-checks"), _length = _checks.length;
                //初始化多选框
                if (!$.isEmptyObject(_checks) && _length > 0) {
                    checksFun();
                    //初始化全选按钮关联
                    if (!$.isEmptyObject(obj.selectAllId) && typeof obj.selectAllId != 'undefined') {
                        allSelete(obj.selectAllId, function (status) {
                            if (status) {
                                $("#" + obj.contextId).find('.i-checks').iCheck('check');
                            } else {
                                $("#" + obj.contextId).find('.i-checks').iCheck('uncheck');
                            }
                        });
                        _checks.on('ifChanged', function (event) {
                            var _len = $("#" + obj.contextId + " div").filter('.checked').length;
                            if (event.target.checked) {
                                _len++;
                            } else {
                                _len--;
                            }
                            if (_len == _length) {
                                $("#" + obj.selectAllId).iCheck('check');
                            } else {
                                $("#" + obj.selectAllId).iCheck('uncheck');
                            }
                        });
                    }
                }
                if (obj.callback != 'undefined' && $.isFunction(obj.callback)) {
                    obj.callback(obj.pageNo);
                }
            },
            error: function (error) {
                icModale(0, '系统错误，数据加载失败');
            }
        });
    }


    function setEndTrTd() {
        //设置最后一行的显示标签
        var list = $(".table-bordered");
        if (list != null && list.length > 0) {
            $.each(list, function (index, em) {
                var trList = $(em).find("tr");
                if (trList != null && trList.length > 0) {
                    var endTr = trList[trList.length - 1];
                    $(endTr).find('[data-toggle="tooltip"]').data("placement", "top");
                    var tdList = $(endTr).find('td');
                    if (tdList != null && tdList.length > 0) {
                        var endTd = tdList[tdList.length - 1];
                        $(endTd).find("a").data("placement", "left");
                    }
                }
            });
        }
    }

});

